package exampleQuestions;

public class PalindromLinkedList {

	static class Node {
		int val;
		Node next;

		public Node(int v) {
			this.val = v;
		}
	}

	/**
	 * how do you test the numbers present in a linked list is palindrome or not
	 * ? (list is a singly linked list) 23
	 */
	public static void main(String[] args) {

		Node n = new Node(5);
		Node n2 = new Node(6);
		Node n3 = new Node(5);
		Node n4 = new Node(5);
		Node n5 = new Node(5);
		Node n6 = new Node(6);
		Node n7 = new Node(5);
		
		n.next = n2;
		n2.next = n3;
		n3.next = n4;
		n4.next = n5;
		n5.next = n6;
		n6.next = n7;

		System.out.println(isPalindrom(n));

	}

	public static boolean isPalindrom(Node n) {

			return checkList(n, n) == null;
	}

	private static Node checkList(Node n, Node n2) {
		
		if (n != null) {
			Node current = checkList(n.next, n2);
			if (current != null && current.val == n.val) {
				return current.next;
			} else {
				return current;
			}
		} else {
			return n2;
		}
	}

}
